package com.nanhang.controller;

import com.nanhang.common.util.PageResult;
import com.nanhang.common.util.R;
import com.nanhang.entity.Employees;
import com.nanhang.service.EmployeesService;
import com.nanhang.vo.EmployeesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/employees")
public class EmployeesController {
    @Autowired
    private EmployeesService employeesService;
    //员工列表
    @GetMapping("/list")
    public R findAll(){
        List<Employees> list = employeesService.list();
        return R.ok().data("list",list);
    }

    @GetMapping("/findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable("page") Integer page,
                        @PathVariable("pageSize") Integer pageSize) {
        PageResult<Employees> pr =employeesService.findByPage(page,pageSize);
        return R.ok().data("pr",pr);
    }
    // 条件带分页查询
    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page,
                    @PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) EmployeesVo vo){
        PageResult<Employees> pr = employeesService.search(page,pageSize,vo);
        return R.ok().data("pr",pr);
    }

    // 添加
    @PostMapping("/add")
    public R add(@RequestBody Employees employees){
        boolean flag = employeesService.add(employees);
        return flag ? R.ok().message("添加成功!") : R.error().message("添加失败!");
    }

    //修改学生
    @PutMapping("/update")
    public R update(@RequestBody Employees employees){
        boolean flag = employeesService.updateById(employees);
        return flag ? R.ok().message("修改学生成功!") : R.error().message("修改学生失败!");
    }


    // 删除学生
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") String ids){  // 1,2,3
        String[] split = ids.split(",");
        List<String> idss = Arrays.asList(split);
        return employeesService.removeByIds(idss) ? R.ok().message("删除成功!") : R.error().message("删除失败!");
    }

}
